package com.huiyujinrong.qiangdanbao.adapter;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.activity.my.OrderStatusActivity;
import com.huiyujinrong.qiangdanbao.bean.OderBean;
import com.huiyujinrong.qiangdanbao.network.Api;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;
import com.huiyujinrong.qiangdanbao.utils.SharedHelper;
import com.huiyujinrong.qiangdanbao.utils.WebViewActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OderAdapter extends BaseAdapter {


    private List<OderBean.ListBean> bean;
    private Context context;

    public OderAdapter(List<OderBean.ListBean> bean, Context context) {
        this.bean = bean;
        this.context = context;
    }

    @Override
    public int getCount() {
        return bean == null ? 0 : bean.size();
    }

    @Override
    public Object getItem(int position) {
        return bean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_orders, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(bean.get(position).getCustomer_name());   //客户姓名
        holder.date.setText(DataUtils.getDate(bean.get(position).getGrab_time()));   //时间

        //借款状态  1:工薪族  2：企业主
        //状态码[1：已发单、2：信贷经理已抢单、3：审核通过、4：审核拒绝、5：放款成功]
        //4  5  状态  不显示那个订单状态icon
        switch (bean.get(position).getLoan_status()) {
            case 1:
                holder.order_status.setVisibility(View.VISIBLE);
                holder.type.setText("已发单");
                break;
            case 2:
                holder.order_status.setVisibility(View.VISIBLE);
                holder.type.setText("审核中");
                break;
            case 3:
                holder.order_status.setVisibility(View.VISIBLE);
                holder.type.setText("审核通过");
                break;
            case 4:
                holder.order_status.setVisibility(View.GONE);
                holder.type.setText("审核拒绝");
                break;
            case 5:
                holder.order_status.setVisibility(View.GONE);
                holder.type.setText("放款成功");
                break;
        }

        holder.jiekuan.setText(bean.get(position).getApply_amt() + " 万元");
        holder.yue.setText(bean.get(position).getLoan_term() + " 个月");


        /**
         * 订单状态 点击事件
         */
        holder.order_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OrderStatusActivity.class);
                intent.putExtra("loan_status",bean.get(position).getLoan_status());
                intent.putExtra("order_id",bean.get(position).getId());
                context.startActivity(intent);
            }
        });

        /**
         * 订单详情点击事件
         */
        holder.check_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();

                bundle.putString("url", Api.HOST + "/jqb_qdb/Customer_details.html" +
                        "?app_kry=ZQXQ-HYQDB" +
                        "&source=1" +
                        "&client_id=" + BorrowHelpApp.get().getPhoneIMEI() +
                        "&channel=" + BorrowHelpApp.get().getChannel() +
                        "&app_version=" + BorrowHelpApp.get().getVersion() +
                        "&order_id=" + bean.get(position).getId());
                bundle.putInt("mm", 4);
                Intent intent = new Intent(context, WebViewActivity.class);
                SharedHelper.put(context, "uuser_id", bean.get(position).getUser_id());
                SharedHelper.put(context, "id", bean.get(position).getId());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.tvOrderName)
        TextView name;
        @BindView(R.id.tvOrderData)
        TextView date;
        @BindView(R.id.tvOrderType)
        TextView type;
        @BindView(R.id.tvOrderMoney)
        TextView jiekuan;
        @BindView(R.id.tvOrderMonth)
        TextView yue;

        @BindView(R.id.order_status)
        LinearLayout order_status;
        @BindView(R.id.check_status)
        LinearLayout check_status;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
